export class infoUtil{

    public static displayErrorInfo(message:string)
    {
        // step init: 获取bod节点
        let root =  document.getElementsByTagName("body")[0];
 
        // step 1： clean error_info elem
        let errorInfoElem = document.getElementById("error_info");
        if(errorInfoElem)
        {
            root.removeChild(errorInfoElem);
        }
        if(message.length == 0) {
          return;
        }

        // step 2: create new error_info elem
        errorInfoElem = document.createElement("p");
        errorInfoElem.id = "error_info";
        errorInfoElem.style = `
    color: red;
    position: fixed;
    z-index: 1;
    bottom: 0;
    left: 0;
    background: bisque;
    padding: 2px 4px;
    margin: 0;
        `;
        errorInfoElem.innerHTML = message;

        // step 3: append error_info elem to root
        root.appendChild(errorInfoElem); 
    }

  public static formatFileSize(size: number|undefined) {
  if (size === undefined) {
    return "N/A";
  }
  const units = ["B", "KB", "MB", "GB", "TB"];
  let unitIndex = 0;
  while (size >= 1024 && unitIndex < units.length - 1) {
    size /= 1024;
    unitIndex++;
  }
  return `${size.toFixed(2)} ${units[unitIndex]}`;
}


 public static async downloadFileWithProgress(url:string, fileName:string, onProgress?: (progress: number, received: number, total: number) => void) {
    try {
        const response = await fetch(url);
        const contentLength = Number(response.headers.get('Content-Length')??'0'); // 总大小（字节）
        let receivedLength = 0; // 已下载大小（字节）
        const chunks = [];

        const reader = response.body!.getReader();

        while (true) {
            const { done, value } = await reader.read();
            if (done) break;

            chunks.push(value);
            receivedLength += value.length;

            // 计算进度百分比
            const progress = contentLength === 0 ? 0 :( receivedLength / contentLength);
            if (onProgress) onProgress(progress, receivedLength, contentLength);
        }

        // 合并 Blob 并触发下载
        const blob = new Blob(chunks);
        const downloadUrl = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = downloadUrl; 
        a.download = fileName;
        document.body.appendChild(a);
        a.click();

        // 清理
        URL.revokeObjectURL(downloadUrl);
        document.body.removeChild(a);
    } catch (error) {
        console.error('Download failed:', error);
    }
}  

public static convertStatus(status: string): string { 
    switch (status) {
      case 'waiting':
        return '等待上传';
      case 'uploading':
        return '上传中';
      case 'success':
        return '上传成功';
      case 'error':
        return '上传失败';
      default:
        return '未知状态';
    }
  
}


public static convertDownloadStatus(status: string): string { 
    switch (status) {
      case 'waiting':
        return '等待下载';
      case 'downloading':
        return '下载中';
      case 'success':
        return '下载成功';
      case 'error':
        return '下载失败';
      default:
        return '未知状态';
    }
  
}

public static convertGvStatus(status: string): string { 
    switch (status) {
      case 'waiting':
        return '等待下载';
      case 'downloading':
        return '下载中';
      case 'compressing':
        return '压缩中';
      case 'success':
        return '下载成功';
      case 'error':
        return '下载失败';
      default:
        return '未知状态';
    }
  }
  


}